ValTrace is an extension tool suite based on Valgrind, designed to analyze and profile function execution during program runtime. It offers two distinct analysis modes to help developers gain in-depth insights into program execution flow and performance characteristics.
ValTrace consists of two core modules:
- ๐ ๏ธ
valstat: Collects statistics on all function calls during the program's entire execution. - โฑ๏ธ
valIntervalStat: Collects function execution information within specific time intervals during program runtime.
- Valgrind: Version 3.18.0 or higher
- Python: Version 3.7 or higher
- callgrind_annotate: Part of the Valgrind toolchain
- callgrind_control: Part of the Valgrind toolchain
pip install -r requirements.txt
Ensure that Valgrind and related tools are installed:
valgrind --version
which callgrind_annotate
which callgrind_control
Collects statistics on all functions called during the program's complete execution and generates a comprehensive function execution trace.
from valtrace.valstat.run_valstat import run_valstat
# Specify the binary file path and runtime parameters
BINARYPATH = '/path/to/your/binary'
RUN_PARAMS = "your_parameters"
# Execute the statistics collection
executed_functions = run_valstat(BINARYPATH, RUN_PARAMS, temp_dir="./tmp")
from valtrace.valstat.run_valstat import run_valstat
BINARYPATH = '/mnt/d/MYTOOLS/ValTrace/valtrace/valIntervalStat/testcase/statistics_runtime_func'
RUN_PARAMS = "5"
run_valstat(BINARYPATH, RUN_PARAMS)
Collects function execution information within specific time intervals during program runtime, allowing you to control the start and end points of monitoring.
from valtrace.valIntervalStat.run_valIntervalStat import run_valIntervalStat
# Specify the binary file path and runtime parameters
BINARYPATH = '/path/to/your/binary'
RUN_PARAMS = "your_parameters"
# Execute the interval-based statistics collection
executed_functions = run_valIntervalStat(BINARYPATH, RUN_PARAMS, temp_dir="./tmp")
from valtrace.valIntervalStat.run_valIntervalStat import run_valIntervalStat
BINARYPATH = '/mnt/d/MYTOOLS/ValTrace/valtrace/valIntervalStat/testcase/statistics_runtime_func'
RUN_PARAMS = "5"
run_valIntervalStat(BINARYPATH, RUN_PARAMS)
- Parses ELF file symbol tables (
.symtaband.dynsym). - Retrieves function addresses and symbol information.
- Supports precise function name matching.
- Parses
callgrind_annotateoutput. - Extracts function addresses and names.
- Generates a list of executed functions.
valstat: Controls the full execution flow.valIntervalStat: Controls interactive interval-based monitoring.
- Program Check: Verifies if Valgrind and related tools are available.
- Temporary Directory Management: Creates and cleans up temporary files.
- Valgrind Execution: Uses the
callgrindtool to generate execution traces. - Result Annotation: Uses
callgrind_annotateto process raw data. - Function Analysis: Parses annotated results and extracts function information.
- Result Output: Displays a list of executed functions.
The project includes two test cases to validate the functionality of the tools:
statistics_func.c: A test program with multi-layer function calls.- Compilation command:
gcc -s -no-pie ./statistics_func.c -o ./statistics_func_test -lm
statistics_runtime_func.c: A test program supporting command-line arguments.- Features: Factorial calculations, power operations, recursive multiplication, etc.
test_valstat.py: Tests for thevalstatmodule.test_valIntervalStat.py: Tests for thevalIntervalStatmodule.
After execution, the tool will output results in the following format:
Executed functions [15]:
[0x400500, 0x400520, 0x400540, 0x400560, ...]
Function profiling and statistics collection.
Function statistics may have omissions. For example, in the case of CVE-2023-33476_minidlnad, Valgrind successfully tracks the timevalfix function, but after processing with callgrind_annotate, the trace information for timevalfix disappears.
The exact reason is unclear, but it is speculated that the low code coverage of the timevalfix function may have caused callgrind_annotate to ignore this function.
- Add function trace functionality.
๐ Enjoy analyzing and profiling your programs with ValTrace! ๐